<!DOCTYPE html>
<html>

<head lang="en">
    <title>骡窝日报</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap/css/bootstrap.min.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/news.css"/>
    <script src="/js/plugins/jrender/jrender.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog/dialog.css">
    <script src="/js/plugins/dialog/dialog.min.js"></script>
    <!--<link rel="stylesheet" href="/js/plugins/jquery-confirm/css/jquery-confirm.css">-->
    <!--<script src="/js/plugins/jquery-confirm/js/jquery-confirm.js"></script>-->
    <script src="/js/common.js"></script>
    <script>
        var oPageNum;
        var keyword = sessionStorage.getItem("keyword");
        sessionStorage.removeItem("keyword");
        $(function () {
            oPageNum = $("#page_num");
            var user = sessionStorage.getItem("user");
            user = JSON.parse(user);
            var userId = getParams().userId;
            var currentPage = 1;
            var pages;
            var newses;
            if (keyword) {
                $("#searchInput").val(keyword);
            }
            if (!userId) {
                $.get("/newses", {currentPage: currentPage, keyword: keyword}, function (data) {
                    renderValue(data.list);
                    newses = data.list;
                    pages = data.pages;
                });
                showHeart(user);
                $(window).scroll(function () {
                    if (isEnd()) {
                        if (currentPage < pages) {
                            currentPage++;
                            $.get("/newses", {currentPage: currentPage, keyword: keyword}, function (data) {
                                newses = $.merge(newses, data.list);
                                renderValue(newses);
                            });
                        } else {
                            $(document).dialog({
                                type: "notice",
                                infoText: "已经到底部了",
                                autoClose: 1500,
                                position: "center"
                            });
                        }
                    }
                    showHeart(user);
                })
            } else {
                $.get("/newses", {currentPage: currentPage, userId: userId, keyword: keyword}, function (data) {
                    renderValue(data.list);
                    newses = data.list;
                    pages = data.pages;
                    showHeart(user);
                });
                $(window).scroll(function () {
                    if (isEnd()) {
                        if (currentPage < pages) {
                            currentPage++;
                            $.get("/newses", {
                                currentPage: currentPage,
                                userId: userId,
                                keyword: keyword
                            }, function (data) {
                                newses = $.merge(newses, data.list);
                                renderValue(newses);
                            });
                        } else {
                            $(document).dialog({
                                type: "notice",
                                infoText: "已经到底部了",
                                autoClose: 1500,
                                position: "center"
                            });
                        }
                    }
                    showHeart(user);
                })
            }

            $("#newsCollection").on("click", "i", function () {
                var heart = $(this);
                if (this.className == "fa fa-heart-o") {
                    if (!user) {
                        $(document).dialog({
                            type: "confirm",
                            style: "IOS",
                            titleShow: false,
                            content: "收藏资讯需要登录",
                            onClickConfirmBtn: function () {
                                location.href = "/login.html";
                            }
                        });
                        return;
                    }
                    $(document).dialog({
                        type: "confirm",
                        style: "IOS",
                        titleShow: false,
                        content: "确认收藏资讯?",
                        onClickConfirmBtn: function () {
                            $.post("/newses/" + heart.closest("p").find("input").val() + "/collections", {userId: user.id}, function (data) {
                                heart.removeClass("fa fa-heart-o").addClass("fa fa-heart");
                            })
                        }
                    });
                } else {
                    $(document).dialog({
                        type: "confirm",
                        style: "IOS",
                        titleShow: false,
                        content: "确认取消收藏资讯?",
                        onClickConfirmBtn: function () {
                            $.ajax({
                                type: "DELETE",
                                url: "/newses/" + heart.closest("p").find("input").val() + "/collections/" + user.id,
                                success: function (data) {
                                    heart.removeClass("fa fa-heart").addClass("fa fa-heart-o");
                                    location.reload();
                                }
                            })

                        }
                    });
                }

            })
            $("#searchInput").focus(function () {
                $(".news").hide();
                $("#searchPic").show();
            })

            $("#searchBtn").click(function () {
                sessionStorage.setItem("keyword", $("#searchInput").val());
                location.reload();
            })

            $("._j_cancel").click(function () {
                sessionStorage.setItem("keyword", "");
                location.reload();
            })
        });

        function renderValue(data) {
            $(".news").renderValues({list: data}, {
                handle: function (ele, value) {
                    ele.href = "/newsContent.html?id=" + value;
                },
                setId: function (ele, value) {
                    $(ele).attr("id", value);
                }
            })
        }

        // 显示心心
        function showHeart(user) {
            setTimeout(function () {
                if (user) {
                    $.get("/newses/" + user.id + "/collections", function (data) {
                        $.each(data, function (index, value) {
                            if (value.id == $("#" + value.id).attr("id")) {
                                $("#" + value.id).removeClass("fa fa-heart-o").addClass("fa fa-heart");
                            }
                        })
                    })
                }
            }, 20);
        }

        function myEvent(obj, ev, fn) {
            if (obj.attachEvent) {
                obj.attachEvent('on' + ev, fn);
            } else {
                obj.addEventListener(ev, fn, false);
            }
        }

        myEvent(window, 'load', function () {
            var oRTT = document.getElementById('gototop');
            var pH = 500;
            var timer = null;
            var scrollTop;
            var scrollHeight;
            window.onscroll = function () {
                scrollTop = document.documentElement.scrollTop || document.body.scrollTop;

                //
                scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
                if (scrollHeight - scrollTop < 800) {
                    var page = oPageNum.val();
                }

                if (scrollTop >= pH) {
                    oRTT.style.display = 'block';
                } else {
                    oRTT.style.display = 'none';
                }
                return scrollTop;
            };
            oRTT.onclick = function () {
                clearInterval(timer);
                timer = setInterval(function () {
                    var now = scrollTop;
                    var speed = (0 - now) / 10;
                    speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
                    if (scrollTop == 0) {
                        clearInterval(timer);
                    }
                    document.documentElement.scrollTop = scrollTop + speed;
                    document.body.scrollTop = scrollTop + speed;
                }, 10);
            }
        });
    </script>
</head>

<body>
<div class="search-head">
    <div class="LBtn">
        <a href="/index.html">
            <span><i class="fa fa-chevron-left fa-2x"></i></span>
        </a>
    </div>
    <div class="CBtn">
        <div class="searchBox">
            <a role="button" tabindex="1" class="cancelsearch _j_cancel">取消</a>
            <form class="search-wrap" action="/newses" id="searchForm">
                <input name="keyword" id="searchInput" placeholder="搜索资讯">
                <button class="s-btn" id="searchBtn" type="button"></button>
            </form>
        </div>
    </div>
</div>
<div class="container news">

    <div class="news" id="newsCollection">
        <h6 class="news-title">骡窝日报 </h6>
        <div class="row" render-loop="list">
            <div class="col-6">
                <a href="newsContent.html" render-fun="handle" render-key="list.id">
                    <img class="rounded" width="100%" height="110px"
                         render-src="list.coverUrl">
                </a>
                <div class="caption">
                    <p>
                        <input type="hidden" render-value="list.id">
                        <span render-html="list.newsTime"> </span>
                        <i class="fa fa-heart-o" render-key="list.id" render-fun="setId"></i>
                    </p>
                    <a href="newsContent.html">
                        <p render-html="list.title"></p>
                    </a>
                </div>
            </div>
        </div>
    </div>
</div>
<div style="top:0">
    <div>
        <img src="/img/top.jpg" width="100%" height="auto" style="display: none" id="searchPic"/>
    </div>
</div>
<a href="javascript:" class="backtop" style="z-index: 99999;" id="gototop"></a>
</body>
</html>